package com.jar.exercise.坑爹的面试题;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;

public class 打印二叉树的节点 {

    static TreeNode root = new TreeNode(-1);

    static {
        init();
    }


    public static void main(String[] args) {
        printFromTopToBottom();
    }

    /**
     *  打印二叉树的节点
     */
    public static void printFromTopToBottom() {
        LinkedList<TreeNode> queue = new LinkedList<>();
        ArrayList<Integer> list = new ArrayList<>();

        if (root == null) {
            return;
        }

        TreeNode curNode = root;

        queue.add(curNode);

        while (!queue.isEmpty()) {
            TreeNode temp = queue.poll();
            list.add(temp.val);

            if (temp.left != null) {
                queue.add(temp.left);
            }

            if (temp.right != null) {
                queue.add(temp.right);
            }
        }

        System.out.println(Arrays.toString(list.toArray()));

    }


    static void init(){

        TreeNode leftTemp;
        TreeNode rightTemp;
        TreeNode temp = root;

            temp.left = new TreeNode(2);
            temp.right = new TreeNode(2+1);
            leftTemp = temp.left;

            leftTemp.left = new TreeNode(3);
            leftTemp.right = new TreeNode(3+1);

            rightTemp = temp.right;

        rightTemp.left = new TreeNode(4);
        rightTemp.right = new TreeNode(4+1);


    }
}

class TreeNode {

    int val = 0;

    TreeNode left = null;

    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }

}